// pages/preview/preview.js
var app = getApp();
Page({

  /** 
   * 页面的初始数据
   */
  data: {
    position:'',
    musicStatus: false,  //音乐是否播放
    share:true,
    avatar:'', 
    nickName:'',
    month:'',
    day: '',
    greet: '',
    bgimg: '',
    iscompany:0,
  },
  musicHandle(e) {
    let that = this;
    let musicStatus = that.data.musicStatus;
    if (musicStatus) {  
      that.data.innerAudioContextNew.pause();
      that.data.innerAudioContextNew.onPause(() => {
        console.log('暂停播放');
        that.setData({
          musicStatus: false
        })
      })
    } else {
      that.data.innerAudioContextNew.play();
      that.data.innerAudioContextNew.onPlay(() => {
        console.log('开始播放');
        that.setData({
          musicStatus: true
        })
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this    
    var id = options.id;
    var is_received = options.is_received || 0;
    var shareid = options.shareid || 0;
    that.setData({
      id,
      is_received,
      shareid
    })  
  },
  // 返回
  editCard: function () {
    let that = this
    that.data.innerAudioContextNew.stop()
    wx.switchTab({
      url: '/pages/index/index',
    })
  },
  // 发送朋友
  sendFriends: function () {
    let that = this
    that.data.innerAudioContextNew.stop()  
    let share = this.data.share
    that.setData({
      share:false
    }) 
  },
  // 点击阴影
  closeHandle: function () {
    let share = this.data.share
    this.setData({
      share: true
    })
  },
  actionSheetbindchange: function () {
    this.setData({
      actionSheetHidden: !this.data.actionSheetHidden
    })
  },
  // 生成卡片
  postersHandle: function () {
    let share = this.data.share
    this.setData({
      share: true
    })
    var that = this
    wx.navigateTo({
      url: '/pages/posters/posters?id=' + that.data.id,
    })
  },

  util: function (obj) {
    console.log(obj);
    console.log(-obj.list);
    var continueTime = (parseInt(obj.list / obj.container) + 1) * 8000;
    var setIntervalTime = 200 + continueTime;

    var animation = wx.createAnimation({
      duration: 200,  //动画时长
      timingFunction: "linear", //线性
      delay: 0  //0则不延迟
    });
    this.animation = animation;
    animation.translateY(obj.container).step({ duration: 200, timingFunction: 'step-start' }).translateY(-obj.list + 10).step({ duration: continueTime });
    this.setData({
      animationData: animation.export()
    })
    setInterval(() => {
      animation.translateY(obj.container).step({ duration: 200, timingFunction: 'step-start' }).translateY(-obj.list + 10).step({ duration: continueTime });
      this.setData({
        animationData: animation.export()
      })
    }, setIntervalTime)
  },
  getHeight() {
    var obj = new Object();
    //创建节点选择器 
    var query = wx.createSelectorQuery();
    query.select('.greeting_wrap').boundingClientRect()
    query.select('.text').boundingClientRect()
    query.exec((res) => {
      obj.container = res[0].height; // 框的height
      obj.list = res[1].height; // list的height
      // return obj;
      console.log(obj.container)
      console.log(obj.list)
      this.util(obj);
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let that = this
    // var position = wx.getStorageSync('position');
    // that.setData({
    //   position: position
    // })

    var id = that.data.id;
    var is_received = that.data.is_received || 0;
    var shareid = that.data.shareid || 0;
    var uid = 0;
    if (id) {
        var intval = setInterval(function(){
          uid = wx.getStorageSync('user_id');
          if(uid){
            clearInterval(intval);
            that.setData({
              id: id,
              is_received: is_received,
              shareid: shareid,
            })
            wx.request({
              url: app.siteinfo.rootpath + 'gethk_over',
              data: { id: id, shareid: that.data.shareid || 0, uid: uid },
              success: function (res) {
                let innerAudioContextRecord = wx.createInnerAudioContext();  //创建录音音频对象
                let innerAudioContext = wx.createInnerAudioContext();  //创建音频对象
                innerAudioContextRecord.autoplay = true;
                innerAudioContext.loop = true;
                if (res.data.code == 200) {
                  setTimeout(function () {
                    that.getHeight()
                  }, 10000);
                  var content = res.data.info.zhufu.split("@#$");
                  content.push(['——',res.data.info.username,'恭贺'])
                  if (content[0]) {
                    that.setData({
                      greet: content,
                    })
                  } else {
                    that.setData({
                      greet: content[0],
                    })
                  }

                  innerAudioContext.src = res.data.info.music; // 设置音频地址
                  innerAudioContextRecord.src = res.data.info.audio; // 设置音频地址
                  innerAudioContext.onPlay(() => {
                    console.log('开始播放')
                  })
                  innerAudioContext.onError((res) => {
                    console.log(res.errMsg)
                    console.log(res.errCode)
                  })
                  if (innerAudioContextRecord.src) {
                    that.setData({
                      hasaudio: false
                    })
                    innerAudioContextRecord.onPlay(() => {
                      console.log('录音开始播放')
                    })
                    innerAudioContextRecord.onError((res) => {
                      console.log(res.errMsg)
                      console.log(res.errCode)
                    })
                    innerAudioContextRecord.onEnded((res) => {
                      console.log('录音播放结束')
                      that.setData({
                        musicStatus: true
                      }, () => { innerAudioContext.play() })
                    })
                  } else {
                    that.setData({
                      musicStatus: true,
                      hasaudio: false
                    }, () => { innerAudioContext.play() })
                  }

                  that.setData({
                    avatar: res.data.info.logo,
                    nickName: res.data.info.username,
                    bgimg: res.data.info.bgimg,
                    innerAudioContextRecordNew: innerAudioContextRecord,
                    innerAudioContextNew: innerAudioContext,
                    position: res.data.info.position
                  })
                  // 获取系统时间
                  var myDate = new Date();
                  let monthNew = myDate.getMonth()
                  let dayNew = myDate.getDate()
                  var month = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
                  that.setData({
                    month: month[monthNew],
                    day: dayNew
                  })
                } else {
                  wx.switchTab({
                    url: '/pages/index/index',
                  })
                }
              }
            })
          }
        },100)
    }    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    this.data.innerAudioContextNew.stop()
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    this.data.innerAudioContextNew.stop()
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    let share = this.data.share
    this.setData({
      share: true
    })
    var that = this
    var userinfo = wx.getStorageSync('userinfo')
    var title = userinfo.wechat_name+'给你送来了一份祝福';
    var path = '/pages/preview/preview?id=' + this.data.id + '&shareid=' + userinfo.id;
    var bgimg = this.data.bgimg
    console.log(path)
    return {
      title: title,
      path: path,
      imageUrl: bgimg,
      success(e) {
        
      }
    }
    
  }
})